<?php
session_start();
include "../connect.php";
include "../functions.php";
require_once("class.upload.php");

$url = getBaseUrl();

$output = json_encode(array("status"=>"error","message"=>"nok"));
  	
if((isset($_POST['step']))&&($_POST['step']=='process')){
		
	$pic = $_SESSION['userName'];
	$handle = new Upload($_SERVER['DOCUMENT_ROOT'].$_POST['tempfile']);
	if ($handle->uploaded) {
		$handle->file_src_name_body      = $pic; // hard name
		$handle->file_overwrite 		 = true;
		$handle->file_auto_rename 		 = false;
		$handle->image_resize            = true;
		$handle->image_convert			 = 'png';
		$handle->image_x                 = 156; //size of final picture
		$handle->image_y                 = 141; //size of final picture
		
		$handle->jcrop                   = true;
		$handle->rect_w                  = $_POST['w']; 
		$handle->rect_h                  = $_POST['h']; 
		$handle->posX                    = $_POST['x']; 
		$handle->posY                    = $_POST['y'];
		//$handle->jpeg_quality 		 	 = 100;
		$handle->Process($_SERVER['DOCUMENT_ROOT'].'/media/userPicture/crop/');
		
		//$handle->clean(); 
		$output = json_encode(array("status"=>"error","message"=>"w"));
		$abfrage = "UPDATE ffxivc_users SET picture = '".$url."/media/userPicture/crop/".$pic.".png' WHERE userId = '".$_SESSION['userId']."'";
		$res = mysql_query($abfrage);
		if ($res){
			$output = json_encode(array("status"=>"success","message"=>$url."/media/userPicture/crop/".$pic.".png"));
		}else{
			$output = json_encode(array("status"=>"error","message"=>mysql_error()));
		}	
			
	}

}
	echo $output;
?>